<?php
class Admin_Form_ValidateGroup{

	private $_arrError;
	
	private $_arrData;
	
	public function __construct($arrParam = null, $options = null){
		
		/*=======================================================
		 * Start - validate name
		 *=======================================================*/	
		$validator = new Zend_Validate();
		
		if($arrParam['action'] == 'add'){			
			$options = array('table'=>'user_group','field'=>'name');
		}elseif($arrParam['action'] == 'edit'){			
			$options = array('table'=>'user_group',
							 'field'=>'name',
							 'exclude'=>array('field'=>'id',
						 				 	  'value'=>$arrParam['id']));
		}
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zend_Validate_Alnum(true),true)
				  ->addValidator(new Zend_Validate_StringLength(array('min'=>3,'max'=>50,'encoding'=>'utf-8')),true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		
		if(!$validator->isValid(trim($arrParam['name']))){
			$message = $validator->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}	
			
		/*=======================================================
		 * Start - validate priority
		 *=======================================================*/	
		$validator = new Zend_Validate();
		$pattern = '#^[0-9]*$#'; //1-n
		
		$validator->addValidator(new Zend_Validate_Regex($pattern),true);
		
		if(!$validator->isValid($arrParam['priority'])){
			$message = $validator->getMessages();
			$this->_arrError['priority'] = current($message);
			$arrParam['priority'] = '';
		}
		
		/*=======================================================
		 * Start - validate avatar
		 *=======================================================*/
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('avatar');
		$fileName = $fileInfo['avatar']['name'];
	
		if(!empty($fileName)){
			$options = array('gif','png','jpg');
			$upload->addValidator('Extension',true,$options,'avatar');
// 			 	   ->addValidator('Size',true,array('min'=>'5kb','max'=>'50kb'),'avatar');
				
			if(!$upload->isValid('avatar')){
				$message = $upload->getMessages();
				$this->_arrError['avatar'] = current($message);
				$arrParam['avatar'] = '';
			}
		}else{
			if($arrParam["action"] == 'edit'){
				$arrParam['avatar'] = $arrParam['current_avatar'];
			}
		}
		
		/*=======================================================
		 * Start - validate ranking
		 *=======================================================*/
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('ranking');
		$fileName = $fileInfo['ranking']['name'];
	
		if(!empty($fileName)){
			$options = array('gif','png','jpg');
			$upload->addValidator('Extension',true,$options,'ranking');
// 			 	   ->addValidator('Size',true,array('min'=>'5kb','max'=>'50kb'),'ranking');
				
			if(!$upload->isValid('ranking')){
				$message = $upload->getMessages();
				$this->_arrError['ranking'] = current($message);
				$arrParam['ranking'] = '';
			}
		}else{
			if($arrParam["action"] == 'edit'){
				$arrParam['ranking'] = $arrParam['current_ranking'];
			}
		}
		
		/*=======================================================
		 * Start - validate ordering
		 *=======================================================*/	
		$validator = new Zend_Validate();
		$pattern = '#^[0-9]*$#'; //1-n
		
		$validator->addValidator(new Zend_Validate_Regex($pattern),true);
		
		if(!$validator->isValid($arrParam['ordering'])){
			$message = $validator->getMessages();
			$this->_arrError['ordering'] = current($message);
			$arrParam['ordering'] = '';
		}
		
		/*=======================================================
		 * Start - return correct data array
		 *=======================================================*/
		$this->_arrData = $arrParam;
	}
	
	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		
		}
		return $this->_arrData;
	}
	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0){
			$flag = true;
		}
		return $flag;
	}
	
	public function getMessageErrors(){
		return $this->_arrError;
	}
	
	public function upload(){
		echo '<br>' . __METHOD__;
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'],
													 	  'fileConfig'=>'user-group.ini'));
		$avatarConfig = $config->getConfig('avatar');
		
		
		/*=======================================================
		 * Start - upload avatar
		 *=======================================================*/
		
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('avatar');
		$fileName = $fileInfo['avatar']['name'];
	
		if(!empty($fileName)){		
			$options = array('rename'=> true,'prefix'=>'avatar_');			
			$fileName = $upload->upload($avatarConfig['uploadDir'],'avatar',$options);
			
			$arrFile['avatar'] = $fileName;
			
			$img = Zendvn_File_Image::create($avatarConfig['uploadDir'] . '/' . $fileName);
			$img->resize($avatarConfig['thumb-width'],$avatarConfig['thumb-height'])
				->save($avatarConfig['thumbDir']. '/' . $fileName);
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($avatarConfig['uploadDir'],$this->_arrData['current_avatar']);
				$upload->remove($avatarConfig['thumbDir'],$this->_arrData['current_avatar']);
			}
		
		}
		
		/*=======================================================
		 * Start - upload ranking
		 *=======================================================*/
		$rankingConfig = $config->getConfig('ranking');
		
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('ranking');
		$fileName = $fileInfo['ranking']['name'];
	
		if(!empty($fileName)){		
			$options = array('rename'=> true,'prefix'=>'ranking_');			
			$fileName = $upload->upload($rankingConfig['uploadDir'],'ranking',$options);
			$arrFile['ranking'] = $fileName;
			
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($rankingConfig['uploadDir'],$this->_arrData['current_ranking']);
			}
		
		}
		
		return $arrFile;
		
		
	}
}







